package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.FamFamily;
import com.ruoyi.system.domain.FamMember;
import com.ruoyi.system.domain.Tree;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.tool.ReplacementTree;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FamTree;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;


/**
 * 家谱图Controller
 * 
 * @author wk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/system/famTree")
public class FamTreeController extends BaseController
{
    @Autowired
    private IFamTreeService famTreeService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IFamFamilyService familyService;
    @Autowired
    private IFamMemberService famMemberService;
    /**
     * 查询家谱图列表
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:list')")
    @GetMapping("/list")
    public TableDataInfo list(FamTree famTree)
    {
        startPage();
        List<FamTree> list = famTreeService.selectFamTreeList(famTree);
        return getDataTable(list);
    }
    /**
     * 获取成员下拉树列表
     */
    @GetMapping("/memberships")
    public AjaxResult memberships()
    {
        //获取用户id
        //根据用户id获取分支列表
        SysDept sysdept = new SysDept();
        FamFamily famFamily = new FamFamily();
        FamMember famMember = new FamMember();
        Long userId = userService.getUserIdByUserName(getUsername());
        sysdept.setUserId(userId);
        List<SysDept> depts = deptService.selectDeptList(sysdept);
        List<FamFamily> families = familyService.selectFamFamilyList(famFamily);


        //获取每个家谱的id
        //根据家谱id获取成员列表
        List<FamMember> members = famMemberService.selectFamMemberList(famMember);
        List<Tree> list = new ArrayList<>();
        List<Tree> clist = new ArrayList<>();
//        for (FamFamily family : families){
//            Long familyId = family.getFamId();
//            famMember.setFamId(familyId);
//            for (FamMember fammember : famMemberService.selectFamMemberList(famMember)){
//                members.add(fammember);
//                System.out.println(fammember);
//                //list.add(new Tree(Long.toString(fammember.getMemberId()),Long.toString(fammember.getFatherId()),fammember.getMemberName()));
////                System.out.println(list);
//            }
//        }
        for (FamMember member : members){
            String id = Long.toString(member.getMemberId());
            String p_id = String.valueOf(member.getFatherId());
            String name = member.getMemberName();
            list.add(new Tree(id,p_id,name));

        }

        clist = new ReplacementTree().builTree(list);

        String jsonString = JSON.toJSONString(clist);
        System.out.println(jsonString);
        //向前端发送树形json数据
        return AjaxResult.success(clist);


    }
    /**
     * 导出家谱图列表
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:export')")
    @Log(title = "家谱图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FamTree famTree)
    {
        List<FamTree> list = famTreeService.selectFamTreeList(famTree);
        ExcelUtil<FamTree> util = new ExcelUtil<FamTree>(FamTree.class);
        util.exportExcel(response, list, "家谱图数据");
    }

    /**
     * 获取家谱图详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return AjaxResult.success(famTreeService.selectFamTreeByMemberId(memberId));
    }

    /**
     * 新增家谱图
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:add')")
    @Log(title = "家谱图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FamTree famTree)
    {
        return toAjax(famTreeService.insertFamTree(famTree));
    }

    /**
     * 修改家谱图
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:edit')")
    @Log(title = "家谱图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FamTree famTree)
    {
        return toAjax(famTreeService.updateFamTree(famTree));
    }

    /**
     * 删除家谱图
     */
    @PreAuthorize("@ss.hasPermi('system:famTree:remove')")
    @Log(title = "家谱图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(famTreeService.deleteFamTreeByMemberIds(memberIds));
    }
}
